package com.ayf.payment.game.api.utils;

import com.ayf.payment.game.api.exception.ParameterException;
import org.apache.commons.lang.StringUtils;

/**
 * 脱敏工具类
 */
public final class SensitiveDataUtil {

    /**
     * 账号名称进行脱敏
     * @param originAccountName
     * @return
     */
    public static String accountNameDesensitization(String originAccountName){
        String newAccountName = "";
        if (StringUtils.isEmpty(originAccountName)){
            return "";
        }
        int accountNameLen = originAccountName.length();
        if (accountNameLen<=2){
            newAccountName = "*".concat(originAccountName.substring(1));
        } else {
            newAccountName="*".concat(originAccountName.substring(1,accountNameLen-1).concat("*"));
        }
        return newAccountName;
    }

    /**
     * 卡号进行脱敏
     * @param originCardNo
     * @return
     */
    public static String cardNoDesensitization(String originCardNo){
        try {
        String newAccountName = "";
        if (StringUtils.isEmpty(originCardNo)){
            return "";
        }
        int accountNoLen = originCardNo.length();
        if (accountNoLen>=11){
            String before=originCardNo.substring(0,6);
            int middelLen=originCardNo.substring(6,accountNoLen-4).length();
            for (int i=0;i<middelLen;i++){
                before=before.concat("*");
            }
            String after=originCardNo.substring(accountNoLen-4);
            newAccountName = before.concat(after);

        } else {
            newAccountName = accountNameDesensitization(originCardNo);
        }
        return newAccountName;
        }catch (Exception e){
            throw new ParameterException("银行卡号不合法");
        }
    }

    /**
     * 支付宝账号脱敏
     * @param originAlipayAccountNo
     * @return
     */
    public static String alipayAccountNoDesensitization(String originAlipayAccountNo){
        try {
            String newVal = "";
            if (StringUtils.isEmpty(originAlipayAccountNo)) {
                return "";
            }
            int accountNoLen = originAlipayAccountNo.length();
            if (accountNoLen<=7){
                return accountNameDesensitization(originAlipayAccountNo);
            }
            if (originAlipayAccountNo.contains("@")) {
                int postionX = originAlipayAccountNo.lastIndexOf("@");
                String before = originAlipayAccountNo.substring(0, 3);
                int middelLen = originAlipayAccountNo.substring(3, postionX).length();
                for (int i = 0; i < middelLen; i++) {
                    before = before.concat("*");
                }
                String after = originAlipayAccountNo.substring(postionX);
                newVal = before.concat(after);
            } else {
                String before = originAlipayAccountNo.substring(0, 3);
                int middelLen = originAlipayAccountNo.substring(3, accountNoLen - 4).length();
                for (int i = 0; i < middelLen; i++) {
                    before = before.concat("*");
                }
                String after = originAlipayAccountNo.substring(accountNoLen - 4);
                newVal = before.concat(after);
            }
            return newVal;
        }catch (Exception e){
            throw new ParameterException("支付宝账号不合法");
        }

    }

    /**
     * 手机号脱敏
     *
     * @param mobile
     * @return
     */
    public static String mobileEncrypt(String mobile) {
        if (StringUtils.isEmpty(mobile) || (mobile.length() != 11)) {
            return mobile;
        }
        return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }
    public static void main(String[] args) {
//        String accountName = SensitiveDataUtil.accountNameDesensitization("11");
//        System.out.println("账号名称脱敏测试:"+accountName);

//        String cardNo = SensitiveDataUtil.cardNoDesensitization("你好");
//        System.out.println("卡号脱敏测试:"+cardNo);

//        String alipayAccountNo = SensitiveDataUtil.alipayAccountNoDesensitization("123456");
//        System.out.println("支付宝邮箱账号脱敏测试:"+alipayAccountNo);
//
        String alipayAccountNo = SensitiveDataUtil.alipayAccountNoDesensitization("17601001780");
        System.out.println("支付宝手机号账号脱敏测试:"+alipayAccountNo);
    }
}
